// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// w [author] rhyme_lph
// E [email]  rhymelph@gmail.com
// h [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/business_contact_entity.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/phone_number_input_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:finance_app/src/widgets/select/normal_check_box.dart';
import 'package:finance_app/src/widgets/switch/normal_switch.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/contact/business_contact_profile_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "businessContactProfilePage")
class BusinessContactProfilePage extends StatefulWidget {
  final BusinessContactEntity data;
  final String name;

  const BusinessContactProfilePage({Key key, this.data, this.name})
      : super(key: key);

  @override
  _BusinessContactProfilePageState createState() =>
      _BusinessContactProfilePageState();
}

class _BusinessContactProfilePageState extends State<BusinessContactProfilePage>
    with
        PagePresenter<BusinessContactProfilePage,
            BusinessContactProfileProvider> {
  String get title => widget.data == null
      ? S.current.new_business_contact
      : provider.isEdit == true
          ? widget.data == null
              ? S.current.edit_business_contact
              : S.current.edit + ' ' + widget.data?.name
          : S.current.business_contacts;

  Widget get rightButton => listBuilder<bool>(
        selector: (_, __) => __.isEdit,
        builder: (BuildContext context, bool value, Widget child) =>
            widget.data != null && provider.isEdit == false
                ? _buildPopButton()
                : _buildSaveBtn(),
      );

  Widget _buildPopButton() => listBuilder<bool>(
      selector: (_, __) => __.isEdit,
      builder: (BuildContext context, bool value, Widget child) =>
          value == true ? Container() : child,
      child: NormalMorePopupButton(
        onSelect: provider.onSelectMenu,
        menuList: provider.menuList,
      ));

  Widget _buildSaveBtn() {
    return listBuilder<bool>(
      selector: (_, __) => pageStatus != PageStatus.loading,
      builder: (BuildContext context, bool value, Widget child) => value == true
          ? NormalSaveTextButton(
              onTap: provider.onSave,
            )
          : Container(),
    );
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          titleWidget: listBuilder<bool>(
            selector: (_, __) => __.isEdit,
            builder: (BuildContext context, bool value, Widget child) =>
                Text(title),
          ),
          actions: [
            rightButton,
          ]);

  @override
  void initState() {
    if (widget.name != null) {
      provider.nameController.text = widget.name;
    }
    provider.country = Config.defaultCountry;

    provider.initProfile(widget.data);
    super.initState();
  }

  @override
  Widget wrapperBody(Widget child) {
    return listBuilder<bool>(
        selector: (_, __) => __.isEdit == true || provider.id == null,
        builder: (BuildContext context, bool value, Widget child) =>
            FormEditStatusInheritedWidget(
              isEdit: value,
              child: child,
            ),
        child: child);
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            FromInputField(
              tip: S.current.business_name,
              controller: provider.nameController,
              focusNode: provider.nameFocusNode,
            ),
            PhoneNumberInputForm(
              controller: provider.phoneNumberController,
              focusNode: provider.phoneNumberFocusNode,
              countryCodeCallback: provider.updateCountryCode,
              defaultCountryCode: provider.countryCode,
            ),
            FromInputField(
              tip: S.current.emailoptional,
              controller: provider.emailController,
              focusNode: provider.emailFocusNode,
              keyboardType: TextInputType.emailAddress,
            ),
            FromInputField(
              tip: S.current.websiteoptional,
              controller: provider.websiteController,
              focusNode: provider.websiteFocusNode,
              keyboardType: TextInputType.url,
            ),
            NormalFormWrapper(
              child: _buildBusinessType(),
            ),
            FormStatusWidget(
              enable: AppSpaces.grayH6,
              disable: listBuilder<bool>(
                selector: (_, __) => __.hasBillingAddress,
                builder: (BuildContext context, bool value, Widget child) =>
                    value == true ? AppSpaces.grayH6 : Container(),
              ),
            ),
            NormalFormWrapper(
              bottomPadding: getDp(20),
              topPadding: getDp(20),
              child: FormStatusWidget(
                enable: PressedOpacityButton(
                  onPressed: provider.onChangeBillAddress,
                  child: listBuilder<bool>(
                    selector: (_, __) => __.hasBillingAddress,
                    builder: (BuildContext context, bool value, Widget child) =>
                        NormalSwitch(
                      title: S.current.billing_address,
                      isSelect: value,
                    ),
                  ),
                ),
                disable: listBuilder<bool>(
                    selector: (_, __) => __.hasBillingAddress,
                    builder: (BuildContext context, bool value, Widget child) =>
                        Visibility(
                          visible: value,
                          child: child,
                        ),
                    child: Text(
                      S.current.billing_address,
                      style: TextStyle(
                        color: aceBlackColor,
                        fontSize: getSp(18),
                        fontWeight: FontWeight.w700,
                        height: 18 / 14,
                      ),
                    )),
              ),
            ),
            listBuilder<bool>(
                selector: (_, __) => __.hasBillingAddress,
                builder: (BuildContext context, bool value, Widget child) =>
                    value == true ? child : Container(),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    AppSpaces.grayH05,
                    listBuilder<String>(
                      selector: (_, __) => __.country?.name,
                      builder:
                          (BuildContext context, String value, Widget child) =>
                              ValueSelectForm(
                        onTap: provider.onSelectCountry,
                        title: S.current.country,
                        value: value,
                      ),
                    ),
                    // FromInputField(
                    //   controller: provider.cityController,
                    //   focusNode: provider.cityFocusNode,
                    //   tip: S.current.city,
                    // ),
                    listBuilder<String>(
                      selector: (_, __) => __.address,
                      builder:
                          (BuildContext context, String value, Widget child) =>
                              ValueSelectForm(
                        onTap: provider.onSelectAddress,
                        title: S.current.address,
                        value: value,
                      ),
                    ),
                    FromInputField(
                      controller: provider.unitNoController,
                      focusNode: provider.unitNoFocusNode,
                      tip: S.current.unit_no,
                    ),
                    FromInputField(
                      controller: provider.postalCodeController,
                      focusNode: provider.postalCodeFocusNode,
                      tip: S.current.postal_code,
                    ),
                  ],
                ))
          ],
        ),
      );

  Widget _buildBusinessType() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        FormStatusWidget(
          enable: listBuilder<bool>(
            selector: (_, __) => __.isClient,
            builder: (BuildContext context, bool value, Widget child) =>
                PressedOpacityButton(
              onPressed: provider.onSelectClient,
              child: NormalCheckBox(
                isSelect: value,
                title: S.current.client,
                minWidth: getDp(140),
              ),
            ),
          ),
          disable: listBuilder<bool>(
            selector: (_, __) => __.isClient,
            builder: (BuildContext context, bool value, Widget child) =>
                value == true
                    ? NormalCheckBox(
                        isSelect: value,
                        title: S.current.client,
                        isEnable: false,
                        minWidth: getDp(140),
                      )
                    : Container(),
          ),
        ),
        FormStatusWidget(
          enable: listBuilder<bool>(
            selector: (_, __) => __.isSupport,
            builder: (BuildContext context, bool value, Widget child) =>
                PressedOpacityButton(
              onPressed: provider.onSelectSupport,
              child: NormalCheckBox(
                isSelect: value,
                title: S.current.supplier,
                minWidth: getDp(140),
              ),
            ),
          ),
          disable: listBuilder<bool>(
            selector: (_, __) => __.isSupport,
            builder: (BuildContext context, bool value, Widget child) =>
                value == true
                    ? NormalCheckBox(
                        isSelect: value,
                        title: S.current.supplier,
                        isEnable: false,
                        minWidth: getDp(140),
                      )
                    : Container(),
          ),
        ),
      ],
    );
  }
}
